﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using ICD10WebApiOData.DTO;
using ICD10WebApiOData.Models;

namespace ICD10WebApiOData
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                  name: "DefaultApiWithAction",
                  routeTemplate: "api / {controller} / {Action} / {ID}",
                  defaults: new { id = RouteParameter.Optional }
              );

            config.EnableQuerySupport();
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<ICDChapter>("ICD10Chapter");
            builder.EntitySet<ChapterContain>("ICDChapterContain");
            builder.EntitySet<ICDDetail>("ICDDetail");
            
            ActionConfiguration getChapterContainByChapID = builder.Entity<ChapterContain>().Collection.Action("GetChapterContainByChapID");
            getChapterContainByChapID.Parameter<string>("ChapID");
            getChapterContainByChapID.Returns<ICDChapterContain>();

            ActionConfiguration getICDHeaderChapID = builder.Entity<ICDDetail>().Collection.Action("GetICDHeaderChapID");
            getICDHeaderChapID.Parameter<int>("ID");
            getICDHeaderChapID.Returns<ICDHeader>();


            ActionConfiguration getICDHeaderByParent = builder.Entity<ICDDetail>().Collection.Action("GetICDHeaderByParent");
            getICDHeaderByParent.Parameter<string>("ParentID");
            getICDHeaderByParent.Returns<ICDHeader>();

            config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
        }
    }
}
